package com.aaa.controller.sds;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.aspect.LogMonitor;
import com.aaa.controller.BaseController;
import com.aaa.entity.PageSortEntity;
import com.aaa.entity.Result;
import com.aaa.entity.sds.QuestionSds;
import com.aaa.service.sds.QuestionSdsService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author ：周文达（3220083246@qq.com）
 * @date ：Created in 2022/8/29 21:18
 * @description：
 * @modified By：
 * @version:
 */
@Controller
@RequestMapping("/questionSds")
public class QuestionSdsController extends BaseController {
    @Autowired
    QuestionSdsService questionSdsService;

    //查询所有
    @ResponseBody
    @RequestMapping("/queryAll.do")
    public Result queryAll(PageSortEntity page, QuestionSds questionSds) {
        Page ppc = (Page) questionSdsService.queryAll(page, questionSds);
        return success(ppc.getResult(), ppc.getTotal());
    }

    //添加
    @LogMonitor
    @ResponseBody
    @RequestMapping("/insert.do")
    public Result insert(QuestionSds questionSds) {
        boolean b = questionSdsService.insert(questionSds);
        if (b) {
            return success();
        }
        return fail();
    }

    //删除
    @LogMonitor
    @ResponseBody
    @RequestMapping("/delete.do")
    public Result delete(String id) {
        boolean b = questionSdsService.delete(id);
        if (b) {
            return success();
        }
        return fail();
    }

    //修改
    @LogMonitor
    @ResponseBody
    @RequestMapping("/update.do")
    public Result update(QuestionSds questionSds) {
        boolean update = questionSdsService.update(questionSds);
        if (update) {
            return success();
        }
        return fail();
    }

    //name 唯一性校验
    @ResponseBody
    @RequestMapping("/repetitionName.do")
    public Result repetitionName(QuestionSds questionSds) {
        if (ObjectUtil.isEmpty(questionSds.getQuestion())) {
            return fail("不能为空");
        } else {
            boolean b = questionSdsService.repetitionName(questionSds);
            if (b) {
                return success("题目名可用");
            }
            return fail("题目重复不可用");
        }
    }

    //ID 唯一性校验
    @ResponseBody
    @RequestMapping("/repetitionID.do")
    public Result repetitionID(QuestionSds questionSds) {
        if (ObjectUtil.isEmpty(questionSds.getId())) {
            return fail("不能为空");
        } else {
            boolean b = questionSdsService.repetitionID(questionSds);
            if (b) {
                return success("ID可用");
            }
            return fail("ID重复不可用");
        }
    }
}